package com.example.springboot_study.controller;

import com.example.springboot_study.entity.primary.User;
import com.example.springboot_study.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : liqun
 * @createTime : 2021/9/13 13:44
 */
@Api(value = "用户相关", tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "创建用户",notes = "向数据库插入user")
    @ApiImplicitParam(name = "user",value = "用户详细实体user",required = true,dataType = "User")
    @PostMapping("/user")
    public int create(@RequestBody User user) {
        /*//获取任务发布的时间
        Date date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        String date1 = sdf.format(date);
        System.out.println(date1);*/
        return userService.create(user.getName(), user.getAge());
    }

    @ApiOperation(value = "查询用户",notes = "根据name查询user")
    @ApiImplicitParam(name = "name",value = "用户名称name",required = true,dataType = "String")
    @GetMapping("/user/{name}")
    public List<User> getByName(@PathVariable String name) {
        return userService.getByName(name);
    }

    @ApiOperation(value = "删除用户",notes = "根据name删除user")
    @ApiImplicitParam(name = "name",value = "用户名称name",required = true,dataType = "String")
    @DeleteMapping("/user/{name}")
    public int deleteByName(@PathVariable String name) {
        return userService.deleteByName(name);
    }

    @ApiOperation(value = "查询用户列表",notes = "查询全部的user")
    @GetMapping("/user/count")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @ApiOperation(value = "清空全部用户",notes = "清空user表")
    @DeleteMapping("/user/all")
    public int deleteAllUsers() {
        return userService.deleteAllUsers();
    }
}
